//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LmExponentialCorrelationModel.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Parameter.h>
#include <gen/QL/Legacy/Libormarketmodels/LmCorrelationModel.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::CLmExponentialCorrelationModel (UInt64 size, Double rho) : CLmCorrelationModel(CLmExponentialCorrelationModel::typeid)
{
    try
    {
#ifdef HANDLE
        _phLmExponentialCorrelationModel = NULL;
#endif
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size);
        QuantLib::Real _rho = (QuantLib::Real)ValueHelper::Convert (rho);
        _ppLmExponentialCorrelationModel = new boost::shared_ptr<QuantLib::LmExponentialCorrelationModel> (new QuantLib::LmExponentialCorrelationModel ( _size,  _rho ));
        SetLmCorrelationModel (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmExponentialCorrelationModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::CLmExponentialCorrelationModel (boost::shared_ptr<QuantLib::LmExponentialCorrelationModel>& childNative, Object^ owner) : CLmCorrelationModel(CLmExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmExponentialCorrelationModel = NULL;
#endif
	_ppLmExponentialCorrelationModel = &childNative;
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmExponentialCorrelationModel));
}
Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::CLmExponentialCorrelationModel (QuantLib::LmExponentialCorrelationModel& childNative, Object^ owner) : CLmCorrelationModel(CLmExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmExponentialCorrelationModel = NULL;
#endif
	_ppLmExponentialCorrelationModel = new boost::shared_ptr<QuantLib::LmExponentialCorrelationModel> (&childNative);
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmExponentialCorrelationModel));
    _LmExponentialCorrelationModelOwner = owner;
    _LmCorrelationModelOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::CLmExponentialCorrelationModel (CLmExponentialCorrelationModel^ copy) : CLmCorrelationModel(CLmExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmExponentialCorrelationModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLmExponentialCorrelationModel = new boost::shared_ptr<QuantLib::LmExponentialCorrelationModel> (copy->GetShared());
        _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmExponentialCorrelationModel));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::CLmExponentialCorrelationModel (System::Type^ t) : CLmCorrelationModel(CLmExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmExponentialCorrelationModel = NULL;
#endif
	if (!t->IsSubclassOf(CLmExponentialCorrelationModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::CLmExponentialCorrelationModel (QuantLib::Handle<QuantLib::LmExponentialCorrelationModel>& childNative, Object^ owner)  : CLmCorrelationModel(CLmExponentialCorrelationModel::typeid)
{
	_phLmExponentialCorrelationModel = &childNative;
	_ppLmExponentialCorrelationModel = &static_cast<boost::shared_ptr<QuantLib::LmExponentialCorrelationModel>>(childNative.currentLink());
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmExponentialCorrelationModel));
    _LmExponentialCorrelationModelOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::CLmExponentialCorrelationModel (QuantLib::Handle<QuantLib::LmExponentialCorrelationModel> childNative)  : CLmCorrelationModel(CLmExponentialCorrelationModel::typeid)
{
	_phLmExponentialCorrelationModel = &childNative;
	_ppLmExponentialCorrelationModel = &static_cast<boost::shared_ptr<QuantLib::LmExponentialCorrelationModel>>(childNative.currentLink());
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmExponentialCorrelationModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::CLmExponentialCorrelationModel (QuantLib::LmExponentialCorrelationModel childNative)  : CLmCorrelationModel(CLmExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmExponentialCorrelationModel = NULL;
#endif
	_ppLmExponentialCorrelationModel = new boost::shared_ptr<QuantLib::LmExponentialCorrelationModel> (new QuantLib::LmExponentialCorrelationModel (childNative));
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmExponentialCorrelationModel));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::~CLmExponentialCorrelationModel ()
{
    if (_ppLmExponentialCorrelationModel != NULL)
    {
	    delete _ppLmExponentialCorrelationModel;
        _ppLmExponentialCorrelationModel = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::!CLmExponentialCorrelationModel ()
{
    if (_ppLmExponentialCorrelationModel != NULL)
    {
	    delete _ppLmExponentialCorrelationModel;
    }
}
QuantLib::LmExponentialCorrelationModel& Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::GetReference ()
{
    if (_ppLmExponentialCorrelationModel == NULL) throw gcnew NativeNullException ();
	return **_ppLmExponentialCorrelationModel;
}
boost::shared_ptr<QuantLib::LmExponentialCorrelationModel>& Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::GetShared ()
{
    if (_ppLmExponentialCorrelationModel == NULL) throw gcnew NativeNullException ();
	return *_ppLmExponentialCorrelationModel;
}
QuantLib::LmExponentialCorrelationModel* Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::GetPointer ()
{
    if (_ppLmExponentialCorrelationModel == NULL) throw gcnew NativeNullException ();
	return &**_ppLmExponentialCorrelationModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LmExponentialCorrelationModel>& Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::GetHandle ()
{
	if (_phLmExponentialCorrelationModel == NULL)
	{
		_phLmExponentialCorrelationModel = new Handle<QuantLib::LmExponentialCorrelationModel> (*_ppLmExponentialCorrelationModel);
	}
	return *_phLmExponentialCorrelationModel;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::HasNative () 
{
	return (_ppLmExponentialCorrelationModel != NULL);
}

Double Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::Correlation (UInt64 i, UInt64 j, Double t, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j);
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLmExponentialCorrelationModel)->correlation ( _i,  _j,  _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Boolean Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel::IsTimeIndependent::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppLmExponentialCorrelationModel)->isTimeIndependent ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILmExponentialCorrelationModel^ Cephei::QL::Legacy::Libormarketmodels::CLmExponentialCorrelationModel_Factory::Create (UInt64 size, Double rho)
{
    return gcnew CLmExponentialCorrelationModel ( size,  rho);
}
